package cn.openea.perpartner.wxpay.service.impl;

import cn.openea.perpartner.wxpay.entity.PayChange;
import cn.openea.perpartner.wxpay.entity.PayOrder;
import cn.openea.perpartner.wxpay.entity.RefundOrder;
import cn.openea.perpartner.wxpay.mapper.AllOrderMapper;
import cn.openea.perpartner.wxpay.mapper.PayChangeMapper;
import cn.openea.perpartner.wxpay.mapper.PayOrderMapper;
import cn.openea.perpartner.wxpay.mapper.RefundOrderMapper;
import cn.openea.perpartner.wxpay.service.QueryService;
import cn.openea.perpartner.wxpay.utils.Tools;
import cn.openea.perpartner.wxpay.vo.QueryVo;
import cn.openea.perpartner.wxpay.vo.R;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @Name: 提供查询各种支付列表实现
 * @author: Mr.Yun
 * @Created: 2022/10/17 19:49
 */
@Service
@Slf4j
public class QueryServiceImpl implements QueryService {

    @Autowired
    PayOrderMapper payOrderMapper;

    @Autowired
    PayChangeMapper payChangeMapper;

    @Autowired
    RefundOrderMapper refundOrderMapper;

    @Autowired
    AllOrderMapper allOrderMapper;

    @Override
    public R queryPayOrderList(QueryVo queryVo) {
        if(Tools.isBlank(queryVo.getUserid())){
            return R.error("必要参数userid为空");
        }
        IPage<PayOrder> iPage = payOrderMapper.selectPage(new Page<>(queryVo.getPageNum(), queryVo.getPageSize()), createdWrapper(queryVo, false));
        List<PayOrder> payOrderList = iPage.getRecords();
        return R.ok().put("list", payOrderList);
    }

    @Override
    public R queryPayChangeList(QueryVo queryVo) {
        if(Tools.isBlank(queryVo.getUserid())){
            return R.error("必要参数userid为空");
        }
        IPage<PayChange> iPage = payChangeMapper.selectPage(new Page<>(queryVo.getPageNum(), queryVo.getPageSize()), createdWrapper(queryVo, false));
        List<PayChange> payChangesList = iPage.getRecords();
        return R.ok().put("list", payChangesList);
    }

    @Override
    public R queryRefundOrderList(QueryVo queryVo) {
        if(Tools.isBlank(queryVo.getUserid())){
            return R.error("必要参数userid为空");
        }
        IPage<RefundOrder> iPage = refundOrderMapper.selectPage(new Page<>(queryVo.getPageNum(), queryVo.getPageSize()), createdWrapper(queryVo, true));
        List<RefundOrder> refundOrderList = iPage.getRecords();
        return R.ok().put("list", refundOrderList);
    }

    @Override
    public R queryAllOrderList(QueryVo queryVo) {
        if(Tools.isBlank(queryVo.getUserid())){
            return R.error("必要参数userid为空");
        }
        Page<RefundOrder> page = new Page<>(queryVo.getPageNum(), queryVo.getPageSize());
        List<RefundOrder> allOrderList = allOrderMapper.selectAllByPage(page, queryVo);
        return R.ok().put("list", allOrderList);
    }

    @Override
    public R queryOrderByID(String tradeNo, String orderType) {
        if(Tools.isBlank(tradeNo)){
            return R.error("必要参数tradeNo为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("tradeNo", tradeNo);

        // orderType：统一支付=order，商户退款=refund，支付零钱=change，其它或ALL表示查以上三者
        if(Tools.isBlank(orderType)){
            orderType = "ALL";
        }

        R r = R.ok();
        try {
            if("order".equalsIgnoreCase(orderType)){
                // 统一支付订单
                PayOrder order = payOrderMapper.selectOne(queryWrapper);
                r.put("order", order);
            } else if("refund".equalsIgnoreCase(orderType)){
                // 商户退款订单
                RefundOrder order = refundOrderMapper.selectOne(queryWrapper);
                r.put("order", order);
            } else if("change".equalsIgnoreCase(orderType)){
                // 支付零钱订单
                PayChange order = payChangeMapper.selectOne(queryWrapper);
                r.put("order", order);
            } else {
                // 从全部订单中查
                QueryVo queryVo = new QueryVo();
                queryVo.setTradeNo(tradeNo);
                RefundOrder order = allOrderMapper.selectOneOrder(queryVo);
                r.put("order", order);
            }
            if(null == r.getData().get("order")){
                r.put("prompt", "无匹配订单");
                r.put("order", new JSONObject());
            } else {
                r.put("prompt", "匹配到订单");
            }
        } catch (Exception e){
            r = R.error(e.getMessage());
        }
        return r;
    }

    /**
     * 按条件生成QueryWrapper条件
     * @param queryVo 条件
     * @param isRefun 是否为退款单，true表示这退款单
     * @return
     */
    private QueryWrapper createdWrapper(QueryVo queryVo, boolean isRefun) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userid", queryVo.getUserid());
        if(Tools.isNotBlank(queryVo.getTradeStatus())){
            if(isRefun){
                queryWrapper.eq("refundNo", queryVo.getTradeNo());
            } else {
                queryWrapper.eq("tradeNo", queryVo.getTradeNo());
            }
        }
        if(Tools.isNotBlank(queryVo.getTradeStatus())){
            queryWrapper.eq("tradeStatus", queryVo.getTradeStatus());
        }
        if(queryVo.isIdAsc()){
            // 如果这里是按ID升序
            queryWrapper.orderByAsc("id");
        } else {
            queryWrapper.orderByDesc("id");
        }
        return queryWrapper;
    }
}
